package com.example.app21_animation_demo;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private EditText et_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate: ");

        et_username = findViewById(R.id.et_username);
    }

    /**
     * 登录
     * @param view 发生事件的视图对象
     */
    public void login(View view) {
        // 获取输入的文本
        String username = et_username.getText().toString();
        // 判断是否为空
        if (TextUtils.isEmpty(username)) {
            // 显示抖动动画
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.shake);
            et_username.startAnimation(animation);
        } else {
            // 否则，提示登录成功
            Toast.makeText(this, username + "：登录成功", Toast.LENGTH_SHORT).show();
        }
        
    }
}